package com.jiayi.weiphoto.activity;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jiayi.weiphoto.R;
import com.jiayi.weiphoto.entity.XTentity;
import com.jiayi.weiphoto.page.FixedWidthImageView;
import com.jiayi.weiphoto.util.SearchSyncImageLoader;
import com.jiayi.weiphoto.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class TXAdapter extends BaseAdapter{
	List<XTentity> tx=null;
	Context context=null;
//	private ImageLoader mImageLoader;
//	private boolean mBusy = false;
//	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
//	protected ImageLoader imageLoader = ImageLoader.getInstance();
	SearchSyncImageLoader imageLoader;
	DisplayImageOptions options;
	View parentView;
	
	public TXAdapter(Context context,List<XTentity> tx, ListView parentView){
		this.tx=tx;
		this.context=context;
//		mImageLoader = new ImageLoader(context);
//		options = new DisplayImageOptions.Builder()
//		.cacheInMemory()
//		.cacheOnDisc()
//		.build();
		imageLoader = SearchSyncImageLoader.getInstance();
		imageLoader.setCacheRootPath(StringUtil.genCompanyRootPath(context));
		this.parentView = parentView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return tx.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return tx.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		Holder holder = null;
		
		if (view == null) {
			holder = new Holder();
			view = LayoutInflater.from(context).inflate(
					R.layout.tx_item, null);
			holder.iv_tx_item=(FixedWidthImageView) view.findViewById(R.id.iv_tx_item);
			holder.tv_tx_item=(TextView) view.findViewById(R.id.tv_tx_item);
			holder.priceTag = (TextView) view.findViewById(R.id.txt_txitem_price);
			view.setTag(holder);
		} 
		else {
			holder = (Holder) view.getTag();
		}
		holder.iv_tx_item.setTag(MainConstants.HOST_img + tx.get(position).getPicture());
		holder.tv_tx_item.setText(tx.get(position).getSumary().replaceAll("\r\n", " "));
		holder.priceTag.setText(String.format("%sԪ", tx.get(position).getPrice()));
//		imageLoader.loadImage(position, MainConstants.HOST_img + tx.get(position).getPicture(), null);
//		imageLoader.displayImage(MainConstants.HOST_img + tx.get(position).getPicture(), holder.iv_tx_item,options,animateFirstListener);
		if(tx.get(position).getPicture() == null || tx.get(position).getPicture().length() == 0){
			
		}else{
			if(tx.get(position).getCover() == null){
				imageLoader.loadImage(position, MainConstants.HOST_img + tx.get(position).getPicture(), new SearchSyncImageLoader.OnImageLoadListener() {
					
					@Override
					public void onImageLoad(String iconUrl, Bitmap bitmap, int pos) {
						// TODO Auto-generated method stub
						FixedWidthImageView imgView = (FixedWidthImageView) parentView.findViewWithTag(iconUrl);
						if(imgView != null){
							imgView.setImageBitmap(bitmap);
						}
						tx.get(pos).setCover(bitmap);
					}
					
					@Override
					public void onError(Integer t) {
						// TODO Auto-generated method stub
						
					}
				});
			}else{
				holder.iv_tx_item.setImageBitmap(tx.get(position).getCover());
			}
		}
		
		
		return view;
	}
	
	static class Holder {
		FixedWidthImageView iv_tx_item;
		TextView tv_tx_item;
		TextView priceTag;
	}
	
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//			if (loadedImage != null) {
//				ImageView imageView = (ImageView) view;
//				boolean firstDisplay = !displayedImages.contains(imageUri);
//				if (firstDisplay) {
//					FadeInBitmapDisplayer.animate(imageView, 500);
//					displayedImages.add(imageUri);
//				} else {
//					imageView.setImageBitmap(loadedImage);
//				}
//			}
		}
	}

}
